<?php

class Utils{
    public static function isIe6(){
        return (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE);
    }

    public static function rebuildUrl($url){
        $url_p = explode('/',$url);
        array_shift($url_p); #throw junk
        $module = array_shift($url_p);
        $action = array_shift($url_p);
        $tmp = array();
        $j = count($url_p)/2;
        for($i=0;$i<$j;$i++){
            $k = array_shift($url_p);
            $v = array_shift($url_p);
            $tmp[$k]=$v;
        }
        $url = "/$module/$action";
        foreach ($tmp as $k=>$v){
            $url .= "/$k/$v";
        }
        return $url;
    }
    
	public static function xml2array($xmlString,$fromNodeName)
	{
		$c = explode('<'.$fromNodeName.'>',$xmlString);

		//print_r($c);die();

		$data = array();

		foreach($c as $i=>$v){
			if($i==0) continue;
			$v = substr($v,0,strpos($v,"</$fromNodeName>"));
			$v = eregi_replace('<[a-zA-Z ]+/>','',$v);
			$v = eregi_replace('</[a-z]+>',"@#\n",$v);
			//$v = str_replace('><',">\n<",$v);
			//print $v."\n";
			$a = explode("@#\n",$v);
			//print_r($a);
			foreach($a as $b){
				if(empty($b) || strpos($b,'>')===false ) continue;

				$key = substr($b,strpos($b,'<')+1,strpos($b,'>')-strpos($b,'<')-1);
				$value = substr($b,strpos($b,'>')+1);

				
				
				$data[$i][$key] = $value;
			}			
		}
		//print_r($data);
		return $data;
	}
	
	public static function addNullTag($str)
	{
	    if(empty($str))
	    {
	        return 'null';
	    }
	    return $str;
	}	
	
	public static function delNullTag($str)
	{
	    if('null' == $str)
	    {
	        return null;
	    }
	    return $str;
	}
	
	public static function array_convert_encoding($in, $out, $arr)
	{
	    if(!is_array($arr))
	    {
	        return iconv($in, $out, $arr);
	    }
	    else
	    {
	        foreach ($arr as $k => $v)
	        {
	            $arr[$k] = self::array_convert_encoding($in, $out, $v);
	        }
	    }
	    return $arr;
	}
    
}